package com.bang.concurrency.example.aqs;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

/**
 * @Auther: Bang
 * @Date: 2019/10/4 19:13
 * @Description:
 */
@Slf4j
public class SemaphoreExample2 {

    private static int threadCount = 20;

    public static void main(String[] args) throws InterruptedException {

        ExecutorService executorService = Executors.newCachedThreadPool();
        final Semaphore semaphore = new Semaphore(6);
        for (int i = 0; i < threadCount; i++) {
            final int threadNum = i;
            executorService.execute(() -> {
                try {
                    semaphore.acquire(3);// 获取3个许可
                    test(threadNum);
                    semaphore.release(3);// 释放3个许可
                } catch (Exception e) {
                    e.printStackTrace();
                }

            });
        }
        executorService.shutdown();
        log.error("finish");
    }

    private static void test(int threadNum) throws InterruptedException {
        log.error("{}", threadNum);
        Thread.sleep(1000);

    }
}
